<?php

class Uuch_Reader_SuningReader extends Uuch_Reader_ProductReader{
    
    public function getInfo($url) {
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();
        $body = $response->getBody();
        
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //Name    #itemprop="name">([\W\w.]+?)<#i   
        $product = new Application_Model_Product();
        $matches = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
            $product->Name = strip_tags($matches[0]);
        }

        $who = '';
        $vendorCode = '';
        $shopType   = '';
        if(preg_match('/vendorCode"\:".*?"/', $body,$matches)){
            $matches = explode(":", $matches[0]);
            $matches        = str_replace('"', "", $matches[1]);
            $vendorCode = $matches;
        }
        if(preg_match('/shopType"\:".*?"/', $body,$matches)){
            $matches = explode(":", $matches[0]);
            $matches        = str_replace('"', "", $matches[1]);
            $shopType = $matches;
        }

        if($vendorCode === "-1"){
            $who = "";
        }else{
            if($shopType === "0"){
                $who ="0000000000";
            }
        }

        if(preg_match('/partNumber"\:"\d*/', $body,$matches)){
            $matches = explode(":", $matches[0]);
            $matches = substr($matches[1],1);
            $html = file_get_contents("http://www.suning.com/webapp/wcs/stores/ItemPrice/{$matches}_{$who}_9051_10346_1.html");
            if(preg_match('/promotionPrice"\:".*?"/', $html,$matches)){
                $matches        = explode(":", $matches[0]);
                $matches        = str_replace('"', "", $matches[1]);
                $product->Price = $matches;
            }
        }else{
            if(preg_match($config->PriceRegex, $body,$matches)){
                $price          = strip_tags($matches[0]);
                $price          = str_replace("&yen;", "", $price);
                $product->Price = $price;
            }
        }
        if(!$product->Price){
            if(preg_match('/partNumber"\:"\d*/', $body,$matches)){
                $matches = explode(":", $matches[0]);
                $matches = substr($matches[1],1);
                $html = file_get_contents("http://www.suning.com/webapp/wcs/stores/GeneralPrice/{$matches}_{$who}_9051_10346_1.html");
                if(preg_match('/promotionPrice"\:".*?"/', $html,$matches)){
                    $matches        = explode(":", $matches[0]);
                    $matches        = str_replace('"', "", $matches[1]);
                    $product->Price = $matches;
                }
            }
        }

        /*if(!$product->Price){
            $newUrl = str_replace("prd","csl", $url);
            $html = file_get_contents($newUrl);
            $prires = json_decode($html);
            $product->Price = $prires[0][0]->productPrice;
        }*/
                
        $product->ShipFee = 0;        
        
        //img           #id="xgalleryImg" src="(.+?)" data-#im
        
        if(preg_match($config->ImageRegex, $body,$matches)){
            if(preg_match('/http.+?"/', $matches[0],$matches)){
                $product->RemoteImage = $product->Image = substr($matches[0], 0, strlen($matches[0])-1);
            }
        }
        
        /*if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }*/
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }
        
        $product->Sku = $id;
        $product->Url = $url;
        
        
        $product->Quantity = 999;
                
        
        $shopName = '苏宁易购';        
        
        $shop = Application_Model_Shop::getByName($shopName);
        if (NULL == $shop){
            $shop = new Application_Model_Shop();
            $shop->Name = $shopName;
            $shop->IsTaobao = True;
            $shop->Url = $uri['host'];
            $shop->save();
        }
        $product->ShopId = $shop->Id;
        $product->save();
        return $product;
    }
    
    public function getInfoForNew($url) {
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();
        $body = $response->getBody();
        
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //Name    #itemprop="name">([\W\w.]+?)<#i   
        $product = new Application_Model_Product();
        $matches = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
            $product->Name = strip_tags($matches[0]);
        }

        $who = '';
        $vendorCode = '';
        $shopType   = '';
        if(preg_match('/vendorCode"\:".*?"/', $body,$matches)){
            $matches = explode(":", $matches[0]);
            $matches        = str_replace('"', "", $matches[1]);
            $vendorCode = $matches;
        }
        if(preg_match('/shopType"\:".*?"/', $body,$matches)){
            $matches = explode(":", $matches[0]);
            $matches        = str_replace('"', "", $matches[1]);
            $shopType = $matches;
        }

        if($vendorCode === "-1"){
            $who = "";
        }else{
            if($shopType === "0"){
                $who ="0000000000";
            }
        }

        if(preg_match('/partNumber"\:"\d*/', $body,$matches)){
            $matches = explode(":", $matches[0]);
            $matches = substr($matches[1],1);
            $html = file_get_contents("http://www.suning.com/webapp/wcs/stores/ItemPrice/{$matches}_{$who}_9051_10346_1.html");
            if(preg_match('/promotionPrice"\:".*?"/', $html,$matches)){
                $matches        = explode(":", $matches[0]);
                $matches        = str_replace('"', "", $matches[1]);
                $product->Price = $matches;
            }
        }else{
            if(preg_match($config->PriceRegex, $body,$matches)){
                $price          = strip_tags($matches[0]);
                $price          = str_replace("&yen;", "", $price);
                $product->Price = $price;
            }
        }
        if(!$product->Price){
            if(preg_match('/partNumber"\:"\d*/', $body,$matches)){
                $matches = explode(":", $matches[0]);
                $matches = substr($matches[1],1);
                $html = file_get_contents("http://www.suning.com/webapp/wcs/stores/GeneralPrice/{$matches}_{$who}_9051_10346_1.html");
                if(preg_match('/promotionPrice"\:".*?"/', $html,$matches)){
                    $matches        = explode(":", $matches[0]);
                    $matches        = str_replace('"', "", $matches[1]);
                    $product->Price = $matches;
                }
            }
        }
        if (!$product->Price) {
            return NULL;
        }

        /*if(!$product->Price){
            $newUrl = str_replace("prd","csl", $url);
            $html = file_get_contents($newUrl);
            $prires = json_decode($html);
            $product->Price = $prires[0][0]->productPrice;
        }*/
                
        $product->ShipFee = 0;        
        
        //img           #id="xgalleryImg" src="(.+?)" data-#im
        
        if(preg_match($config->ImageRegex, $body,$matches)){
            if(preg_match('/http.+?"/', $matches[0],$matches)){
                $product->RemoteImage = $product->Image = substr($matches[0], 0, strlen($matches[0])-1);
            }
        }
        
        /*if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }*/
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }
        
        $product->Sku = $id;
        $product->Url = $url;
        
        
        $product->Quantity = 999;
                
        
        $shopName = '苏宁易购';
        
        return array(
            'product' => $product, 
            'shopName' => $shopName,
            'siteName' => '苏宁易购',
            'shopUrl' => $url,
            'url' => $url,
            'siteUrl' => $uri['scheme'] . '://' . $uri['host']);
    }
    
}